package com.example.tsproject.Controller;


import com.example.tsproject.Utils.ThreadLocalUtil;
import com.example.tsproject.entity.Assignments;
import com.example.tsproject.entity.PageBean;
import com.example.tsproject.entity.Result;
import com.example.tsproject.entity.Students;
import com.example.tsproject.service.AssignmentsService;
import com.example.tsproject.service.StudentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/students")
@Slf4j
public class StuController {

    @Autowired
    private StudentsService studentsService;
    @Autowired
    private AssignmentsService assignmentsService;

    @GetMapping
    public Result<Students> getByStuNumber(){

        String StuNumber= ThreadLocalUtil.get();
        System.out.println(StuNumber);
        Students students=studentsService.findStudents(StuNumber);
        System.out.println(students);
        return Result.success(students);

    }

    @GetMapping("/findAll")
    public Result<PageBean<Students>> findAllStudents(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String name, //根据作者姓名查询
            @RequestParam(required = false) String id, //根据学号查询
            @RequestParam(required = false) String stuClass //根据班级询
    ) {
        PageBean<Students> pageBean = studentsService.findAllStudents(pageNum, pageSize, name, id, stuClass);
        if (pageBean != null) {
            return Result.success(pageBean);
        } else {
            return Result.error("Students not found");
        }
    }

    @GetMapping("/delete")
    public Result delete(
            String id
    ) {
        studentsService.delete(id);
        return Result.success();
    }

    @GetMapping("/add")
    public Result add(
            String student_number,
            String name,
            String password,
            String stuclass,
            String email
    ) {
        System.out.println(student_number);
        studentsService.add(student_number, name, password, stuclass, email);
        System.out.println(student_number);
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Students students){
        studentsService.updateById(students);
        System.out.println("*******************");
        System.out.println(students);
        return Result.success();
    }
    @GetMapping("/getExerciseList")
    public Result getExerciseList(){
        String stuNumber=ThreadLocalUtil.get();
        String type="type3";
//        stuNumber="22222222";
//        String publisher_Number=stuNumber;
        System.out.println(stuNumber);
//        List<Assignments> list=assignmentsService.getExerciseList(publisher_Number);
        List<Assignments> list=assignmentsService.getAllTypeList(stuNumber,type);
        System.out.println("----------------");
        System.out.println(list);
        return Result.success(list);
    }
    @GetMapping("/getHomeworkList")
    public Result getHomeworkList(){
        String stuNumber=ThreadLocalUtil.get();
        String type="type1";
//        stuNumber="22222222";
        System.out.println(stuNumber);
//        List<Assignments> list=assignmentsService.getHomeworkList(stuNumber);
        List<Assignments> list=assignmentsService.getAllTypeList(stuNumber,type);
        System.out.println("----------------");
        System.out.println(list);
        return Result.success(list);
    }
    @GetMapping("/getExamList")
    public Result getExamList(){
        String stuNumber=ThreadLocalUtil.get();
        String type="type2";
//        stuNumber="22222222";
        System.out.println(stuNumber);
//        List<Assignments> list=assignmentsService.getHomeworkList(stuNumber);
        List<Assignments> list=assignmentsService.getAllTypeList(stuNumber,type);
        System.out.println("----------------");
        System.out.println(list);
        return Result.success(list);
    }
    @GetMapping("/getTestList")
    public Result getTestList(){
        String stuNumber=ThreadLocalUtil.get();
        String type="type4";
//        stuNumber="22222222";
        System.out.println(stuNumber);
//        List<Assignments> list=assignmentsService.getHomeworkList(stuNumber);
        List<Assignments> list=assignmentsService.getAllTypeList(stuNumber,type);
        System.out.println("----------------");
        System.out.println(list);
        return Result.success(list);
    }







}
